package com.example.lending.View;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class PersonFragment extends Fragment {
    EditText et_sex,et_class,et_user_name,et_user_price,et_user_count,et_user_inrom,et_user_pub;
    EditText et_user_type;
    Button btn_yes;
    UserViewModel userViewModel;

    private User user;
    public PersonFragment() {
        // Required empty public constructor
    }
    public void inteView(){
        et_sex=getView().findViewById(R.id.et_person_sex);
        et_class=getView().findViewById(R.id.et_person_class);
        et_user_count=getView().findViewById(R.id.et_person_count);
        et_user_name=getView().findViewById(R.id.et_person_name);
        et_user_price=getView().findViewById(R.id.et_person_price);
        et_user_inrom=getView().findViewById(R.id.et_person_intro);
        et_user_pub=getView().findViewById(R.id.et_person_pub);
        et_user_type=getView().findViewById(R.id.et_person_type);
        btn_yes=getView().findViewById(R.id.btn_book_update);
        userViewModel= ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication(),getActivity())).get(UserViewModel.class);
    }

    public void getThisUser(){
        String name = et_user_name.getText().toString();
        String type = et_user_type.getText().toString();
        String price = et_user_price.getText().toString();
        String count = et_user_count.getText().toString();
        String pub = et_user_pub.getText().toString();
        String intro = et_user_inrom.getText().toString();
        String  clazznumber = et_class.getText().toString();
        String  sex = et_sex.getText().toString();

        int i=0;
        if(!TextUtils.isEmpty(name)){
            user.setName(name);
            i++;
        }
        if(!TextUtils.isEmpty(type)){
            user.setMajor(type);
            i++;
        }
        if(!TextUtils.isEmpty(count)){
            user.setPassword(count);
            i++;
        }
        if(!TextUtils.isEmpty(intro)){
            user.setEmail(intro);
            i++;
        }
        if(!TextUtils.isEmpty(price)){
            user.setAge(Integer.parseInt(price));
            i++;
        }
        if(!TextUtils.isEmpty(sex)){
            user.setSex(sex);
            i++;
        }
        if(!TextUtils.isEmpty(clazznumber)){
            user.setClassnum(clazznumber);
            i++;
        }
        if(i==7)
            btn_yes.setEnabled(true);

        if(!TextUtils.isEmpty(pub)){
            int level=Integer.parseInt(pub);
            String username=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE).getString("username","");
            int leveluser=getActivity().getSharedPreferences("user",Context.MODE_PRIVATE).getInt("usertype",-1);//后面这个叫缺省值，表示如果没有数据，那赋值一个什么
            if(level<leveluser){
                user.setType(level);
            }else{
                Toast.makeText(getActivity(), "只能设置比自己等级低类型", Toast.LENGTH_SHORT).show();
                user.setType(-1);
            }

        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteView();
        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inteView();
                getThisUser();
                LiveData<List<User>>liveData=userViewModel.findUserWithPattern(user.getName());
                List<User>users= liveData.getValue();
                //查找对应的user，将原本的删除，增加一个新的
                if(user.getType()!=-1){
                    for (int i=0;i<=users.size();i++){
                        userViewModel.deleteUsers(users.get(i));
                    }
                    userViewModel.insertUsers(user);
                }
            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_person, container, false);
    }

}
